﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Prism.Common;
using Prism.Mvvm;

namespace DigitalPlatform.Models
{
    public class DeviceItemModel : BindableBase
    {
        public string DeviceNum { get; set; }
        private bool _isSelected;

        public bool IsSelected
        {
            get { return _isSelected; }
            set { SetProperty(ref _isSelected, value); }
        }

        private double x;
        public double X
        {
            get { return x; }
            set { SetProperty(ref x, value); }
        }
        private double y;
        public double Y
        {
            get { return y; }
            set { SetProperty(ref y, value); }
        }
        private int z = 0;

        public int Z
        {
            get { return z; }
            set { SetProperty(ref z, value); }
        }

        private double _width;
        public double Width
        {
            get { return _width; }
            set { SetProperty(ref _width, value); }
        }
        private double _height;
        public double Height
        {
            get { return _height; }
            set { SetProperty(ref _height, value); }
        }

        // 根据这个名称动态创建一个组件实例
        public string DeviceType { get; set; }


        #region func

        
        public ICommand DeleteCommand { get; set; }

        bool _canmove = false;
        Point point;

        public void Btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _canmove = true;
            point = e.GetPosition((IInputElement)sender);
            Mouse.Capture((IInputElement)sender);
        }
        public void Btn_MouseMove(object sender, MouseEventArgs e)
        {
            if (_canmove)
            {
                //获取到相对父类的坐标，也就是相对于Canvas的鼠标坐标
                var p = e.GetPosition(GetParent((DependencyObject)sender));
                X = p.X - point.X;
                Y = p.Y - point.Y;
            }
        }
        public void Btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _canmove = false;
            Mouse.Capture(null);
        }

        private Canvas GetParent(DependencyObject obj)
        {
            var ctr = VisualTreeHelper.GetParent(obj);
            if (ctr != null && ctr is Canvas)
            {
                return ctr as Canvas;
            }
            return GetParent(ctr);
        }



        #endregion


    }
}
